// too stupid need to be optimised later        ---optimization


export const handleInitPosition = (props) => {
	const { place, bubble, container } = props;
	const childElement = bubble.nextElementSibling as HTMLElement
	var originHorz, originVert, afterHorz, afterVert;

	function top() {
		originHorz = -(bubble['offsetWidth'] / 2 - childElement['offsetWidth'] / 2) +
		(container['offsetWidth'] - childElement['offsetWidth']);

		originVert = - bubble['offsetHeight'] + 4;
		afterHorz = originHorz;
		afterVert = originVert - 4;
	}

	function bottom() {
		originHorz = -(bubble['offsetWidth'] / 2 - childElement['offsetWidth'] / 2) +
		(container['offsetWidth'] - childElement['offsetWidth']);

		originVert = childElement['offsetHeight'] - 4;
		afterHorz = originHorz;
		afterVert = originVert + 4;
	}

	function left() {
		originHorz = -bubble['offsetWidth'] + 4;
		originVert = 0;
		afterHorz = originHorz - 4;
		afterVert = originVert;
	}

	function right() {
		originHorz = childElement['offsetWidth'] + 4;
		originVert = 0;
		afterHorz = originHorz - 4;
		afterVert = originVert;
	}

	if ('top' === place[0]) {
		top();
	}
	if ('bottom' === place[0]) {
		bottom();
	}
	if ('left' === place[0]) {
		left();
	}
	if ('right' === place[0]) {
		right();
	}

	bubble.style.transform = `translate(${originHorz}px, ${originVert}px)`;
	return {
		who: bubble,
		child: childElement,
		ahorz: afterHorz,
		horz: originHorz,
		avert: afterVert,
		vert: originVert
	}
}
